import 'package:flutter/material.dart';
import 'package:flutter_changdian_football/api/api_response.dart';
import 'package:flutter_changdian_football/api/req/match/req_match.dart';
import 'package:flutter_changdian_football/utils/custom_constant.dart';
import 'package:flutter_changdian_football/utils/custom_logger.dart';
import 'package:flutter_changdian_football/utils/event/custom_event_util.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

class MatchController extends GetxController with GetTickerProviderStateMixin {

  TabController? tabController;

  @override
  void onInit() {
    super.onInit();
    tabController = TabController(length: 2, vsync: this);

    getUserMatchNum();

    eventBus.on<BottomNavigationBarEvent>().listen((event) {
      if (event.message == CustomConstant.EVENT_MATCH_REFRESH) {
        EasyLoading.show();
        // 刷新数据
        getUserMatchNum();
      } else if (event.message == CustomConstant.EVENT_LOGOUT_REFRESH) {
        clearData();
      } else if (event.message == CustomConstant.EVENT_REFRESH) {
        EasyLoading.show();
        // 刷新数据
        getUserMatchNum();
      }
    });
  }

  clearData() {
    signingMatchNum = null;
    waitForStartMatchNum = null;
    update();
  }

  /// 报名中数量
  int? signingMatchNum;
  /// 待开始数量
  int? waitForStartMatchNum;

  /// 获取报名中-待开始数量
  getUserMatchNum() async {
    var resp = await ReqMatch.getUserMatchNum();
    if (resp.status == Status.completed) {
      if (resp.data['code'] == '200') {
        signingMatchNum = resp.data['data']['signingMatchNum'];
        waitForStartMatchNum = resp.data['data']['waitForStartMatchNum'];
        update();
      }
    }
  }

}